package binlog;

import binlog.listener.BinlogLifecycle;
import binlog.listener.DataEventListener;
import binlog.model.TableId;
import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.event.*;
import com.github.shyiko.mysql.binlog.jmx.BinaryLogClientStatistics;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Marker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@Slf4j
public class App {
    static BinaryLogClient client = null;

    /**
     * 参考-每一次的更新事件都有- [query,table_map,{insert,update,delete}] 三个连续的事件发生
     * io.debezium.connector.mysql.MySqlStreamingChangeEventSource#handleUpdateTableMetadata
     */
    private final Map<Long, TableId> tableIdsByTableNumber = new ConcurrentHashMap<>();

    public static void main(String[] args) {

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                log.error("i am shutdownHook...");
                if (client != null){
                    try {
                        client.disconnect();
                        log.info("after disconnect");
                    } catch (IOException e) {
                        log.error("{}",e);
                    }
                }
            }
        }));
        SpringApplication.run(App.class, args);
    }

    @Autowired
    BinlogLifecycle binlogLifecycle;
    @Autowired
    DataEventListener dataEventListener;

    @PostConstruct
    public void runBinlogReader(){
//       环境变量(Environment variables): MYSQL_HOST=testmysql.xx.com;MYSQL_PORT=3306;MYSQL_USER=xx;MYSQL_PWD=mP7hcbRBpNvj3r7U
        String host = System.getenv("MYSQL_HOST");
        String port = System.getenv("MYSQL_PORT");
        String user = System.getenv("MYSQL_USER");
        String pwd = System.getenv("MYSQL_PWD");

        new Thread(new Runnable() {
            @Override
            public void run() {
                client = new BinaryLogClient(host, Integer.valueOf(port), user, pwd);
                //内置的-事件统计时间信息
//                client.registerEventListener(new BinaryLogClientStatistics());
                client.registerLifecycleListener(binlogLifecycle);
                client.registerEventListener(dataEventListener);
                try {
                    client.connect();
                } catch (IOException e) {
                    log.error("{}",e);
                }
            }
        },"binlog-reader").start();

    }
}
